<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>定时器</title>
    <style>
      .out div {
        float: left;
        width: 40px;
        height: 40px;
        margin-left: 5px;
        background-color: black;
        text-align: center;
        color: white;
        line-height: 40px;
        font-size: 20px;
      }
    </style>
  </head>
  <body>
    <div class="out">
      <div class="hour">1</div>
      <div class="min">2</div>
      <div class="sec">3</div>
    </div>
    <script>
      var hour = document.querySelector(".hour");
      var min = document.querySelector(".min");
      var sec = document.querySelector(".sec");
      var inputTime = +new Date("2023-1-4 18:00:00");
      //计算时间的函数
      countDown(); //先调用一次，防止一进去的空白
      function countDown() {
        var nowTime = +new Date(); //当前的毫秒
        //用户输入的毫秒
        var times = (inputTime - nowTime) / 1000; //剩余的秒数
        // var d = parseInt(times / 60 / 60 / 24);
        // d = d < 10 ? "0" + d : d;
        var h = parseInt((times / 60 / 60) % 24);
        h = h < 10 ? "0" + h : h;
        hour.innerHTML = h;
        var m = parseInt((times / 60) % 60);
        m = m < 10 ? "0" + m : m;
        min.innerHTML = m;
        var s = parseInt(times % 60);
        s = s < 10 ? "0" + s : s;
        sec.innerHTML = s;
      }
      setInterval(countDown, 1000);
    </script>
  </body>
</html>
